
	** estimate dynamic effects 

***Inputs: 
* $Data/GameUserWPdaily_norm.dta
* $Data/maxmindirectionsmonth_zip260.dta


***Outputs: 
* $Results/dynamic.tex

	use $Data/GameUserWPdaily_norm.dta, clear
	
	gen month = month(date)

		
	gen zip2 = int( zip/10)
	
	merge m:1 zip2 month using $Data/maxmindirectionsmonth_zip260.dta
	
	keep if _merge ==3
	
	
	gen monthyear = mofd(date)

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 
	label variable highpm "Daily PM2.5 $>$ 25"

	
	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360
	
	gen maxzipwind = (cut_winddirection ==maxdirection )
	replace maxzipwind = . if maxdirection==.
	
	keep if maxzipwind<. 
	
	egen cut_temp = cut(weighted_temperature), at(-40(10)40)

	
	eststo clear 
	
	
	forval l = 1(1)7 {
	
	
	gen lag`l'highpm = lag`l'_dailypm > 25 & lag`l'_dailypm<.
	gen lead`l'highpm = lead`l'_dailypm > 25 & lead`l'_dailypm<.
	
	label variable lag`l'highpm "Daily PM2.5 $>$ 25 (t-`l')"
	label variable lead`l'highpm "Daily PM2.5 $>$ 25 (t+`l')"
	
	egen lead`l'cut_winddirection = cut(lead`l'median__closestwind), at(0(60)360)
	replace lead`l'cut_winddirection = 0 if lead`l'median__closestwind ==360

	gen lead`l'maxzipwind = (lead`l'cut_winddirection ==maxdirection )
	replace lead`l'maxzipwind = . if maxdirection==.
	
	
	egen lag`l'cut_winddirection = cut(lag`l'median__closestwind), at(0(60)360)
	replace lag`l'cut_winddirection = 0 if lag`l'median__closestwind ==360

	gen lag`l'maxzipwind = (lag`l'cut_winddirection ==maxdirection )
	replace lag`l'maxzipwind = . if maxdirection==.
	
	

	egen lead`l'cut_temp = cut(lead`l'weighted_temperature), at(-40(10)40)
	egen lag`l'cut_temp = cut(lag`l'weighted_temperature), at(-40(10)40)
	
	
	}
	
	
	
	
	
	
	
	
	ivreghdfe norm_score (highpm lag1highpm lead1highpm =lag1maxzipwind maxzipwind lead1maxzipwind)  weighted_wind_speed weighted_precipitation lead1weighted_wind_speed lead1weighted_precipitation lag1weighted_wind_speed lag1weighted_precipitation, absorb(anon_id monthyear cut_temp lead1cut_temp lag1cut_temp) cluster(anon_id monthyear) ffirst
	
	eststo dynamic1
	
	scalar Ftest = e(first)[8,1]
	
	estadd scalar Ftest , replace

	

	ivreghdfe norm_score (highpm lag*highpm lead*highpm =lag*maxzipwind maxzipwind lead*maxzipwind)  weighted_wind_speed weighted_precipitation lead*weighted_wind_speed lead*weighted_precipitation lag*weighted_wind_speed lag*weighted_precipitation, absorb(anon_id monthyear cut_temp lead*cut_temp lag*cut_temp) cluster(anon_id monthyear) ffirst

	eststo dynamic7
	
	scalar Ftest = e(first)[8,1]
	
	estadd scalar Ftest 
	
	
		test lag1highpm + lag2highpm +  lag3highpm +  lag4highpm  +  lag5highpm +  lag6highpm +  lag7highpm=0
		scalar lagF = r(F)
		estadd scalar lagF 
		
		test lead1highpm + lead2highpm  + lead3highpm + lead4highpm + lead5highpm + lead6highpm +  lead7highpm =0
		scalar leadF = r(F)
		estadd scalar leadF 
		
	
	
	
	
	
	
	ivreghdfe norm_score (highpm   = maxzipwind )  weighted_wind_speed weighted_precipitation lag1maxzipwind lead1maxzipwind lead1weighted_wind_speed lead1weighted_precipitation lag1weighted_wind_speed lag1weighted_precipitation, absorb(anon_id monthyear cut_temp lead1cut_temp lag1cut_temp) cluster(anon_id monthyear) ffirst
	
	eststo dynamic1_cont
	
	scalar Ftest = e(first)[8,1]
	
	estadd scalar Ftest 
	
	
	

	ivreghdfe norm_score (highpm  = maxzipwind ) lag*maxzipwind lead*maxzipwind weighted_wind_speed weighted_precipitation lead*weighted_wind_speed lead*weighted_precipitation lag*weighted_wind_speed lag*weighted_precipitation, absorb(anon_id monthyear cut_temp lead*cut_temp lag*cut_temp) cluster(anon_id monthyear) ffirst

	eststo dynamic7_cont
	
	scalar Ftest = e(first)[8,1]
	
	estadd scalar Ftest 
	
	
	
	
	
	
	
	
	
	
	
	
	
	esttab dynamic1 dynamic7 dynamic1_cont dynamic7_cont using $Results/dynamic.tex, drop(*weighted_wind_speed *weighted_precipitation *maxzipwind) replace se scalars("Ftest First stage F" "lagF Sum lags F" "leadF Sum leads F") nocon star(* 0.1 ** 0.05 *** 0.001) label nomtitles

